import 'dart:core';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

import '../utils/formatter/customized_length_formatter.dart';
import '../utils/formatter/customized_text_formatter.dart';
import 'package:getxdemo/res/style.dart';

class LoginEditWidget extends StatefulWidget {
  final String hint;
  final Widget? leading;
  final ValueChanged<String> valueChanged;
  final bool isPassWord;
  final EdgeInsetsGeometry? margin;

  const LoginEditWidget({
    Key? key,
    this.hint = '',
    this.leading,
    required this.valueChanged,
    this.isPassWord = false,
    this.margin = const EdgeInsets.only(top: 8, bottom: 8, left: 25, right: 25),
  }) : super(key: key);

  @override
  State<LoginEditWidget> createState() => _LoginEditWidgetState();
}

class _LoginEditWidgetState extends State<LoginEditWidget> {
  bool textEmpty = true;
  /// 密码可见
  bool expend = false;

  @override
  Widget build(BuildContext context) {
    return Container(
      margin: widget.margin,
      child: Stack(
        alignment: AlignmentDirectional.center,
        children: [
          TextField(
            style: Styles.style_white_16,
            obscureText: !expend && widget.isPassWord,
            onChanged: (value) {
              widget.valueChanged(value);
              setState(() {
                textEmpty = value.isEmpty;
              });
            },
            decoration: InputDecoration(
                hintText: widget.hint,
                hintStyle: Styles.style_white_16,
                border: _getEditBorder(false),
                focusedBorder: _getEditBorder(true),
                disabledBorder: _getEditBorder(false),
                enabledBorder: _getEditBorder(false),
                contentPadding: const EdgeInsets.only(left: 52, right: 36)),
            inputFormatters: [
              ///输入长度和格式限制
              CustomizedLengthTextInputFormatter(16),
              CustomizedTextInputFormatter(
                filterPattern: RegExp("[a-zA-Z]|[0-9]"),
              ),
            ],
          ),
          /// 图标
          Positioned(
            left: 16,
            child: widget.leading ??
                const Icon(
                  Icons.confirmation_num_sharp,
                  color: Colors.white,
                  size: 24,
                ),
          ),
          /// 分割线
          Positioned(
            left: 46,
            child: Container(
              width: 1,
              height: 22,
              color: Colors.white,
            ),
          ),
          /// 密码可见
          Positioned(
              right: 0,
              child: Visibility(
                  visible: widget.isPassWord && !textEmpty,
                  child: IconButton(
                    onPressed: () {
                      setState(() {
                        expend = !expend;
                      });
                    },
                    icon: Icon(
                      expend ? Icons.remove_red_eye : Icons.visibility_off,
                      color: Colors.white,
                    ),
                  )))
        ],
      ),
    );
  }

  ///获取输入框的Border属性，可公用
  ///[isEdit]是否获取焦点
  OutlineInputBorder _getEditBorder(bool isEdit) {
    return OutlineInputBorder(
      borderRadius: const BorderRadius.all(Radius.circular(30)),
      borderSide: BorderSide(
        color: isEdit ? Colors.green : Colors.white24,
        width: 1,
      ),
    );
  }
}
